package com.pz.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pz.gulimall.product.dao.BrandDao;
import com.pz.gulimall.product.entity.BrandEntity;
import com.pz.gulimall.product.entity.CategoryEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pz.gulimall.common.utils.PageUtils;
import com.pz.gulimall.common.utils.Query;

import com.pz.gulimall.product.dao.CategoryBrandRelationDao;
import com.pz.gulimall.product.entity.CategoryBrandRelationEntity;
import com.pz.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 获取某个 分类 下所有的 品牌
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> getByCatId(Long catId) {
        List<Long> existBrandId = this.list(new QueryWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id",catId)).stream().map(item->{
            return item.getBrandId();
        }).collect(Collectors.toList());

        if(existBrandId.size()>0){
            List<BrandEntity> list = brandDao.selectBatchIds(existBrandId);
            return list;
        }

        return new ArrayList<>();
    }


    public List<CategoryBrandRelationEntity> getByBrandId(Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id", brandId);
        return baseMapper.selectList(queryWrapper);
    }

    public boolean updateWhenBrandChange(BrandEntity brand){
        if(!brand.getName().isEmpty()){
            UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
            //更新 brandName
            updateWrapper.set("brand_name", brand.getName());
            updateWrapper.eq("brand_id",brand.getBrandId());
            return update(updateWrapper);
        }
        return false;
    }

    public boolean updateWhenCategoryChange(CategoryEntity category){
        if(!category.getName().isEmpty()){
            UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
            //更新 brandName
            updateWrapper.set("catelog_name", category.getName());
            updateWrapper.eq("catelog_id",category.getCatId());
            return update(updateWrapper);
        }
        return false;
    }
}